package com.market.cash.http

import com.market.cash.analyse.AnalyseBuilder
import com.market.cash.analyse.AnalyseUtil
import com.market.cash.utils.CashRootDateUtil
import com.market.cash.utils.helper.SharedHelper
import org.json.JSONObject
import java.util.Random

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/16 10:59
 * @Annotation：接口API
 */
object ApiService {

    /**
     * http://159.138.165.72
     * 域名
     */
    const val CASHROOT_BASE_URL = "https://www.maestcre-credit.info"

    /**
     * 接口Url
     */
    object API_URL{
        /**
         * 地区转发
         * /v1/timezonePath
         */
        const val CASHROOT_API_TIMEZONE_URL = "/jzNsYvowTsC87kJkkNWaq"
        /**
         * APP业务统一请求接口
         * /v1/commonApi
         */
        const val CASHROOT_API_COMMON_URL = "/lO4gIW8QvPAo3Zf5MOWzA"
        /**
         * APP业务统一请求接口（登录）
         * /v1/commonApi
         */
        const val CASHROOT_API_LOGIN_URL = "/lFdc3rXwkgN9IJmxtOq2pA"
        /**
         * APP业务统一请求接口（流程）
         * /v1/commonApi
         */
        const val CASHROOT_API_PROCESS_URL = "/4vMa8CWp0jiBe27DfeIvXg"
        /**
         * APP业务统一请求接口（埋点）
         * /v1/commonApi
         */
        const val CASHROOT_API_ADD_POINT_URL = "/nzuGOw03F0G39K7Wqlfbjw"
        /**
         * APP业务统一请求接口（数据分析）
         * /v1/commonApi
         */
        const val CASHROOT_API_ANALYSIS_URL = "/X4F74qPjXU13AHPIjwQ6ww"
        /**
         * APP业务统一请求接口（产品列表）
         * /v1/commonApi
         */
        const val CASHROOT_API_PRODUCT_LIST_URL = "/s3q1ASOeStZ3Kks41JxWYg"
        /**
         * APP业务统一请求接口（自定义数据接口）
         * /v1/commonApi
         */
        const val CASHROOT_API_CUSTOM_DATA_URL = "/4AUEKSco7L3rAsb5LDC2DQ"
        /**
         * 图片上传
         * /v1/imageUpload
         */
        const val CASHROOT_API_UPLOAD_FILE_URL = "/c51XxV3B5esQqDPXKmEheQ"
    }

    /**
     * 数据分析Path
     */
    object ANALYSIS_PATH{

        /**
         * googel install 请求路径
         * /v1/channelInstallTemp
         */
        const val CASHROOT_ANALYSIS_GOOGLE_INSTALL_PATH = "/PaANDYoMUpiAxMT3cmfXbw"

        /**
         * adjust install 请求路径
         * /v1/adjustInstallTemp
         */
        const val CASHROOT_ANALYSIS_ADJUST_INSTALL_PATH = "/aOkzgRfQRdgLG6XxvPahw"

        /**
         * facebook install 请求路径
         * /v1/saveFbAppData
         */
        const val CASHROOT_ANALYSIS_FACEBOOK_INSTALL_PATH = "/MO4Wv1y1SnsMdMBd4Azog"

        /**
         * AppList 请求路径
         * /v2/userAppList
         */
        const val CASHROOT_ANALYSIS_APPLIST_PATH = "/h3jRpqqHiowjW0X13Gs5Zg"

        /**
         * 请求路径
         * /v2/smsRecord
         */
        const val CASHROOT_ANALYSIS_SMS_PATH = "/bVQwhuzH9I8DdfbsDRAmw"

        /**
         * device 请求路径
         * /v1/deviceInfo
         */
        const val CASHROOT_ANALYSIS_DEVICE_PATH = "/gJNhFy9OmIQ3kygXHJadvQ"

        /**
         * battery 请求路径
         * /v1/userDeviceBattery
         */
        const val CASHROOT_ANALYSIS_BATTERY_PATH = "/WbEA8HL548uSTz1qCuiYA"

        /**
         * hardwave 请求路径
         * /v1/userDeviceHardware
         */
        const val CASHROOT_ANALYSIS_HARDWAVE_PATH = "/Lzz7HAXRTE96WjMSgXBsVw"

        /**
         * contact 请求路径
         * /v2/phoneBookList
         */
        const val CASHROOT_ANALYSIS_CONTACT_PATH = "/IM3Yp6CInuP0SpUin4GSBA"

        /**
         * storge 请求路径
         * /v1/userDeviceStorage
         */
        const val CASHROOT_ANALYSIS_STORGE_PATH = "/SQ7SliI9nDSBV6KglmHw"

        /**
         * wifi 请求路径
         * /v1/userDeviceWifi
         */
        const val CASHROOT_ANALYSIS_WIFI_PATH = "/c3k3bZS0I4bbgmTudl6lZg"

        /**
         * calllog 请求路径
         * /v2/callRecord
         */
        const val CASHROOT_ANALYSIS_CALLLOG_PATH = "/c7zweRUt0xS9yhsveWQ6WuQ"
    }


    /**
     * 接口Path
     */
    object API_PATH{
        /**
         * APP配置参数 Path
         * /v1/getAppShowInfo
         */
        const val CASHROOT_API_APP_INFO_PATH = "xMnOQkphiDab8wSxGmrsKA"

        /**
         * 发送验证码
         * /v1/smsCode
         */
        const val CASHROOT_API_SEND_SMS_PATH = "gPpb0RBE1OZFDViP1oMnQ"

        /**
         * 发送whatsapp验证码
         * /v1/sendWhatAppMessage
         */
        const val CASHROOT_API_WHATSAPP_SEND_CODE_PATH = "vKrmGNSSSkBEK5IYFp9lxw"

        /**
         * 校验whatsapp验证码
         *  /v1/checkWhatAppMessageCode
         */
        const val CASHROOT_API_WHATSAPP_CHECK_CODE_PATH = "ytTEdR35BIIsKHQQCOVrnA"

        /**
         * 登录
         * /v1/userLogin
         */
        const val CASHROOT_API_LOGIN_PATH = "SAOvmGsVnmstsKaqAodq8w"

        /**
         * 无验证码登录
         * /v1/userLoginNoCode
         */
        const val CASHROOT_API_LOGIN_NO_CODE_PATH = "5cZkMzPOPp7VO1p1KyPsuw"

        /**
         * 用户流程状态
         * /v1/userProcess
         */
        const val CASHROOT_API_PROCESS_PATH = "ayOF8GabUjOj4CwB4wMkg"

        /**
         * 保存用户图片信息（身份证、护照、驾驶证、人脸）
         * /v1/ocrInfo
         */
        const val CASHROOT_API_OCR_INFO_PATH = "ZQyYmMBwcu10SAEwgXOb5Q"

        /**
         * 用户贷款额度
         * /v1/loanLimit
         */
        const val CASHROOT_API_LIMIT_PATH = "f8NjTC5Io9K0CocxNNzg"

        /**
         * 用户贷款额度信息（循环额度）
         * /v1/getLoanHomePage
         */
        const val CASHROOT_API_LOAN_LIMIT_PATH = "iRMI7g6LNk7Z9MqLblpB4Q"

        /**
         * 新用户贷款额度预览信息
         * /v1/getLoanApplyDetail
         */
        const val CASHROOT_API_LOAN_APPLY_PATH = "FWBPoqhcIYPDwPK1toQ7w"

        /**
         * 可选额度和期限的预览信息(老用户)
         * /getLoanDetailPageForDays
         */
        const val CASHROOT_API_LOAN_APPLY_CUSTOM_LIMIT_PATH = "jAnANz081n8lbTRnHdInwg"

        /**
         * 创建贷款申请订单
         * /v1/loanApply
         */
        const val CASHROOT_API_LOAN_APPLY_ORDER_CREATE_PATH = "CBYVZ9TfJ3fPLxivfGsEA"

        /**
         * 订单列表
         * /v1/getAllOrderList
         */
        const val CASHROOT_API_LOAN_ORDER_PATH = "D8NcIPng3OJxZLRbTY2Uqg"

        /**
         * 贷款还款信息
         * /v1/repayInfo
         */
        const val CASHROOT_API_LOAN_REPAYMENT_INFO_PATH = "LKw7adGq37vSqG6251bwg"

        /**
         * 贷款还款链接
         * /v1/orderRepayH5
         */
        const val CASHROOT_API_LOAN_REPAYMENT_URL_PATH = "c1L25haDX00X1OZncH7wA"

        /**
         * 贷款还款链接（肯尼亚、乌干达）
         * /v1/getOrderRepayH5
         */
        const val CASHROOT_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH = "kf4SYX2SqWv4AGO7jMspDg"

        /**
         * 获取支付状态（没有还款链接时，会进入查询支付状态流程）
         * /v1/queryPaymentStatus
         */
        const val CASHROOT_API_LOAN_REPAYMENT_STATUS_PATH = "vMcwJgdyD6NjwnOHMXikyA"

        /**
         * 账户列表
         * /v1/getBindCardList
         */
        const val CASHROOT_API_ACCOUNT_LIST_PATH = "H7PzSF4pV0QubGv4sZl5Hg"

        /**
         * 已绑定账户列表（法语）
         * /v2/fetchBoundBankAccount
         */
        const val CASHROOT_API_ACCOUNT_LIST_FRENCH_PATH = "FV7GNcdkETIY6dq3bitY6w"

        /**
         * 添加账户
         * /v2/bindBankAccount
         */
        const val CASHROOT_API_ACCOUNT_ADD_PATH = "AJWaSPM0tvqfuSuGFJaLQ"

        /**
         * 删除已绑定的账户
         * /v1/deleteBoundBankAccount
         */
        const val CASHROOT_API_ACCOUNT_DELETE_PATH = "L2NU9vYAQ9AGuU79kUgBQ"

        /**
         * 评价弹框
         * /v1/fetchStarWindowConfig
         */
        const val CASHROOT_API_STAR_COMMENT_PATH = "N3ySqIi8NpUJRIB3XsZESw"

        /**
         * 提交意见反馈
         * /v1/feedback
         */
        const val CASHROOT_API_FEEDBACK_SUBMIT_PATH = "XyAfJpZn2d7oKVyfj6uA"

        /**
         * 推荐APP列表
         * /v1/userDeniedDc
         */
        const val CASHROOT_API_RECOMMEND_APP_LIST_PATH = "c6jRHk4s3hR9MDedKDCxw"
        /**
         * 事件埋点
         * /v1/userBuriedPoint
         */
        const val CASHROOT_API_TRACK_EVENT_PATH = "ZFP95YRIVgcEVYRT35X8A"

        /**
         * 消息列表
         * /v1/fetchUserMessages
         */
        const val CASHROOT_API_MESSAGE_LIST_PATH = "cu2H2wshpcmb91W9fOwkuQ"

        /**
         * 产品列表
         * /queryNewStyleProducts
         */
        const val CASHROOT_API_LOAN_PRODUCT_LIST_PATH = "dUR83bXnRf8mnyS30zTrA"

        /**
         *  复制产品A信息 到 产品B (配置信息、进件信息)
         *  /oneKeyCopyToApply
         */
        const val CASHROOT_API_LOAN_PRODUCT_COPY_PATH = "jVcQH7lWyhMNukpoe3FFA"

        /**
         * 优惠券列表
         * /v1/getUserCouponAndScoreInfo
         */
        const val CASHROOT_API_COUPON_LIST_PATH = "c71Uk0YiDkuXzfcRiLvYMuw"

        /**
         * 签到信息
         * /v1/userSign
         */
        const val CASHROOT_API_SIGN_PATH = "OfGGtrZNizFMIAc4v18w"

        /**
         *  存取积分、优惠券兑换
         *  /v1/changeUserScore
         */
        const val CASHROOT_API_SAVE_OR_USE_PATH = "d845ZgZps25dfjjoaq2NDQ"

        /**
         * 保存自定义参数接口
         * /v1/saveAbUserInfo
         */
        const val CASHROOT_API_SAVE_CUSTOM_PARAMS_PATH = "gWiUZ53QCoaIJfUzWQdzSw"

        /**
         * 获取自定义参数
         * /v1/getAbUserInfo
         */
        const val CASHROOT_API_LOAD_CUSTOM_PARAMS_PATH = "LRlN6dDfK8HOzyNtSxiwEQ"

        /**
         * 清除用户数据
         * /v1/userCancel
         */
        const val CASHROOT_API_USER_DATA_CLEAR_PATH = "/8dveg4DFORD5WI5YtyRZbg"
    }

    /**
     * 获取请求Url
     */
    fun getHttpUrl(url: String): String{
        //地区转发
        val timeZone = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$CASHROOT_BASE_URL/$timeZone$url"
        }
        return "$CASHROOT_BASE_URL$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", AnalyseBuilder.product)                                           //  产品 APP名称
            put("token", SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", AnalyseUtil.getAdId())                                          //  广告id
            put("aduid", AnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_USERID))                                             //  userId
            put("version", AnalyseBuilder.libVersion)                                        //  app版本号
            put("timp",System.currentTimeMillis())                                            //  时间戳
            put("country",SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid",SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE_ID))                                          //当前时区id
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this.addCustomParams()
    }

    /**
     * 添加随机参数增量 4
     */
    private fun JSONObject.addCustomParams(): JSONObject{
        val mobile = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_MOBILE)
        val startTime = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_APP_START_TIME)
        val loginTime = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_LOGIN_TIME)
        val currentTime = CashRootDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")
        apply {
            put("loginMobile",mobile)
            put("startTime",startTime)
            put("loginTime",loginTime)
            put("currentTime",currentTime)
        }
        return this
    }

    /**
     * 随机字符串
     */
    private fun getCashRootRandomKey(length: Int): String {
        val charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        val stringBuilder = StringBuilder()
        val random = Random()
        for (i in 0..length){
            val index = random.nextInt(charSet.length)
            stringBuilder.append(charSet[index]);
        }
        return stringBuilder.toString();
    }
}